$(function () {
    page(null,1,10);
    $("#usearch_submit").click(function (data) {
        if (data!="index.jsp"){
            var name = $("#Name").val()
            page(name,1,10)
        }else {
            location.href = data
        }
    })

    $("#add_submit").click(function () {
        $("#addModal").modal({
            backdrop:"static"
        })
        $("#addSubmit").click(function () {
            $.post("user/insertAdmin",$("#updateFrom").serialize(),function (data) {
                if (data){
                    layer.msg("添加成功！", {time:1000, icon:6, shift:6}, function (e) {
                        $("#addModal").modal('hide')
                        $.ready(page(null,1,10))
                    });
                }else{
                    layer.msg("用户名存在！", {time:1000, icon:5, shift:6}, function (e) {
                        $("#addModal").modal('hide')
                        $.ready(page(null,1,10))
                    });
                }
            })
        })
    })

    /**
     * 用于选中全部
     */
    $("#checkboxs").click(function () {
        var boxs = $(":input[name='checkbox']")
        for (let i = 0; i < boxs.length; i++) {
            boxs[i].checked=this.checked
        }
    })

    /**
     * 用于删除选中
     */
    $("#delete_submit").click(function (data) {
        layer.confirm("您确定要删除选中?",  {icon: 3, title:'提示'}, function(e) {
            $.post("user/deleteByIds", $("#tabForm").serialize(), function (data) {
                window.location.reload();
                window.scrollTo(0, 50);
            })
            layer.close(e)
        });
    })

})

function page(name,current,pageSize) {
    $.post("user/findByAdmin",{name:name,currentPage:current,pageSize:pageSize},function (json) {
        if (json != "index.jsp" ){
            if (json.flag){
                var users = json.data
                $("#totalPage").html(users.totalPage)
                $("#totalCount").html(users.totalCount)
                var user_lis = ''
                for (var i = 0; i < users.list.length; i++) {
                    var user = users.list[i]
                    var li = ""
                    li += '<tr>\n' +
                        '        <td><input name="checkbox" value="'+user.id+'" type="checkbox"></td>\n' +
                        '        <td>'+user.name+'</td>\n' +
                        '        <td>'+user.gender+'&nbsp;</td>\n' +
                        '        <td>'+user.password+'</td>\n' +
                        '        <td>'+user.power+'&nbsp;</td>\n' +
                        '        <td>\n' +
                        '            <button onclick="updateUser('+user.id+','+users.currentPage+')" type="button" class="btn btn-info">修改</button>\n' +
                        '            <button onclick="deleteUser('+user.id+','+users.currentPage+')" type="button" class="btn btn-danger">删除</button>\n' +
                        '        </td>\n' +
                        '    </tr>'
                    user_lis += li
                }
                $("#RTable").html(user_lis)
            }

            var beforeNum = users.currentPage-1;
            if (beforeNum <= 0){
                beforeNum = 1;
            }
            var lis = '<li onclick="javascript:page(\''+name+'\','+1+','+pageSize+')"><a href="javascript:void(0)">首页</a></li>\n' +
                '<li onclick="javascript:page(\''+name+'\','+beforeNum+','+pageSize+')"> <a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">&laquo;</span> </a></li>'
            //开始页码
            let begin;
            //结束页码
            let end;
            if (users.totalPage <= 10){
                begin = 1;
                end = users.totalPage;
            }else {
                begin = (users.currentPage - 5);
                end = (users.currentPage + 4);
                if (begin < 1){
                    begin = 1;
                    end = begin + 9;
                }
                if (end > users.totalPage){
                    end = users.totalPage;
                    begin = end - 9;
                }
            }
            for (let i = begin; i <= end; i++) {
                var l = ""
                if(users.currentPage  == i){
                    l = '<li class="active" onclick="javascript:page(\''+name+'\','+i+','+pageSize+')"><a href="javascript:void(0);">'+i+'</a></li>'
                }else {
                    l = '<li onclick="javascript:page(\''+name+'\','+i+','+pageSize+')"><a href="javascript:void(0);">'+i+'</a></li>'
                }
                lis += l
            }

            var nextNum = users.currentPage +1;
            if (nextNum >= users.totalPage){
                nextNum = users.totalPage;
            }
            lis += '<li onclick="javascript:page(\''+name+'\','+nextNum+','+pageSize+')"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a> </li>\n' +
                '<li onclick="javascript:page(\''+name+'\','+users.totalPage+','+pageSize+')"><a href="javascript:void(0);">尾页</a></li>'

            $("#paging").html(lis)
        }else {
            location.href = json
        }
    })
    window.scrollTo(0,50);

}

/*删除函数*/
function deleteUser(uid,i) {
    layer.confirm("您确定要删除?",  {icon: 3, title:'提示'}, function(e) {
        $.post("user/deleteByAdminId",{aid:uid},function (json) {
            if (json){
                $.ready(page(null,i,10))
            }
        })
        layer.close(e)
    });
}

/*更新查询，并放入模态框*/
function updateUser(id,i) {
    $("#modal").html("<div class=\"modal fade\" id=\"updateModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\">\n" +
        "    <div class=\"modal-dialog\" role=\"document\">\n" +
        "        <div class=\"modal-content\">\n" +
        "            <div class=\"modal-header\">\n" +
        "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n" +
        "                <h4 class=\"modal-title\" id=\"myModalLabel\">用户信息修改</h4>\n" +
        "            </div>\n" +
        "            <div class=\"modal-body\">\n" +
        "                <form id=\"updateFrom\" class=\"form-horizontal\">\n" +
        "                    <input id=\"id\" hidden=\"hidden\" type=\"text\" name=\"id\">\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"uname\" class=\"col-sm-2 control-label\">姓名：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"text\" name=\"name\" class=\"form-control\" id=\"uname\" placeholder=\"name\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"sex1\" class=\"col-sm-2 control-label\">性别：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <label class=\"radio-inline\">\n" +
        "                                <input type=\"radio\" name=\"gender\" checked=\"checked\" id=\"sex1\" value=\"\"男> 男\n" +
        "                            </label>\n" +
        "                            <label class=\"radio-inline\">\n" +
        "                                <input type=\"radio\" name=\"gender\" id=\"sex2\" value=\"女\"> 女\n" +
        "                            </label>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"password\" class=\"col-sm-2 control-label\">密码：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"password\" name=\"password\" class=\"form-control\" id=\"password\" placeholder=\"password\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"flag1\" class=\"col-sm-2 control-label\">权限：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <label class=\"radio-inline\">\n" +
        "                                <input type=\"radio\" name=\"power\" id=\"flag1\" value=\"1\" > 超级管理\n" +
        "                            </label>\n" +
        "                            <label class=\"radio-inline\">\n" +
        "                                <input type=\"radio\" name=\"power\" checked=\"checked\" id=\"flag2\" value=\"0\"> 普通管理\n" +
        "                            </label>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </form>\n" +
        "            </div>\n" +
        "            <div class=\"modal-footer\">\n" +
        "                <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n" +
        "                <button type=\"button\" id=\"updateSubmit\" class=\"btn btn-primary\">Save</button>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>");
    $.post("user/findByAdminId",{id:id},function (json) {
        if (json!="index.jsp"){
            var user = json
            $("#id").attr({value:user.id});
            $("#uname").attr({value:user.name});
            $("#password").attr({value:user.password});
            $("#sex1").removeAttr("checked");
            $("#sex2").removeAttr("checked");
            if (user.gender == "男"){
                $("#sex1").attr({checked:'checked'});
            }else {
                $("#sex2").attr({checked:'checked'});
            }
            $("#flag1").removeAttr("checked");
            $("#flag2").removeAttr("checked");
            if (user.power == 1){
                $("#flag1").attr({checked:'checked'})
            }else {
                $("#flag2").attr({checked:'checked'})
            }

        }else {
            location.href = ''+json+''
        }
    })

    $("#updateModal").modal({
        backdrop:"static"
    })

    $("#updateSubmit").click(function () {
        layer.confirm("您确定要更新?",  {icon: 3, title:'提示'}, function(e) {
            $.post("user/updateAdmin", $("#updateFrom").serialize(), function (data) {
            })
            $("#updateModal").modal('hide')
            $.ready(page(null, i, 10))
            layer.close(e)
        })
    })
}

